<template>
  <div>
    <pro-table
      ref="tableRef"
      :data="tableData"
      :total="total"
      :columns="columns"
      :tool-button="false"
      @search="commEvent.onSearch"
      :page-change="commEvent.onPageChange"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- 顶部配置项 -->
      <template #tableHeader>
        <el-row justify="end">
          <el-button type="primary" icon="Plus" @click="commEvent.onSave()">新增</el-button>
        </el-row>
      </template>
      <!-- 操作 -->
      <template #operation="{ row }">
        <el-button type="primary" link @click="commEvent.onSave(row)">编辑</el-button>
        <el-button type="danger" link @click="commEvent.onDelete(row)">删除</el-button>
      </template>
    </pro-table>
    <!-- 新增编辑弹窗 -->
    <save-config v-model="saveVisible" :rows="currents" @refresh="commEvent.onSearch" />
  </div>
</template>

<script setup lang="ts">
import { h, onMounted, ref } from "vue"
import ProTable from "@/components/ProTable/index.vue"
import SaveConfig from "./components/SaveConfig.vue"
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface"
import { ElMessage, ElMessageBox } from "element-plus"
import { configPage, deleteConfig } from "@/api/modules/system"
import { SystemSpace } from "@/api/interface/system"
// 参数
const tableRef = ref<ProTableInstance>() // 表格实例
const tableData = ref<SystemSpace.ConfigPage[]>([]) // 表格数据
const total = ref<number>(0) // 数据总数
const saveVisible = ref<boolean>(false) // 新增编辑弹窗
const currents = ref<SystemSpace.ConfigPage>() // 当前选中数据
// 页码参数
const pageParams = ref({
  pageIndex: 1,
  pageSize: 10
})
// 表格列配置
const columns: ColumnProps<SystemSpace.ConfigPage>[] = [
  { prop: "paramName", label: "参数名称", isShow: false, search: { el: "input" } },
  { prop: "name", label: "参数名称", width: 180 },
  { prop: "configKey", label: "参数键名", width: 220 },
  { prop: "configValue", label: "参数键值", width: 120 },
  { prop: "type", label: "系统内置", width: 120, render: scope => h("span", scope.row.type === "Y" ? "是" : "否") },
  { prop: "remark", label: "备注" },
  { prop: "operation", label: "操作", width: 120, fixed: "right" }
]
// 事件集合
const commEvent = {
  // 获取表格数据
  getTableData: async (params: SystemSpace.ConfigPageQuery) => {
    const { data } = await configPage(params)
    tableData.value = data.result
    total.value = data.totalCount
  },
  // 查询
  onSearch: () => {
    tableRef.value?.search()
    pageParams.value.pageIndex = 1
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  // 分页
  onPageChange: (page: number, size: number) => {
    pageParams.value.pageIndex = page
    pageParams.value.pageSize = size
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  //  新增/编辑
  onSave: (row?: SystemSpace.ConfigPage) => {
    currents.value = row
    saveVisible.value = true
  },
  // 删除
  onDelete: async (row: SystemSpace.ConfigPage) => {
    ElMessageBox.confirm("确定删除此菜单?", "删除", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(async () => {
        const { code } = await deleteConfig({ id: row.id })
        if (code !== "200") return
        ElMessage.success("删除成功")
        commEvent.onSearch()
      })
      .catch(() => {})
  }
}
onMounted(() => {
  commEvent.getTableData(pageParams.value)
})
</script>

<style lang="scss" scoped></style>
